<?php
/**
 * 为主题提供可用的Filter hooks
 *
 * @package one
 * @subpackage Hooks
 */

	/*
	* Remove WP and plugin functions
	*/
	remove_filter( 'pre_user_description', 'wp_filter_kses' );
	remove_filter( 'pre_term_description', 'wp_filter_kses' );

	/*
	* Feed links
	*/
	add_filter( 'feed_link', 'one_feed_link', 1, 2 );
	add_filter( 'category_feed_link', 'one_other_feed_link' );
	add_filter( 'author_feed_link', 'one_other_feed_link' );
	add_filter( 'tag_feed_link', 'one_other_feed_link' );
	add_filter( 'search_feed_link', 'one_other_feed_link' );

	/*
	* Category and tag links
	*/
	add_filter( 'term_links-post_tag', 'one_post_tag_term_link' );
	add_filter( 'term_links-category', 'one_category_term_link' );
	
	
/**
 * Filters main feed links for the site.
 * This changes the feed links to the user's alternate feed URL.
 * This change only happens if the user chooses it from the theme settings.
 *
 * @since 0.4
 * @return string $output
 */
function one_feed_link( $output, $feed ) {
	global $one_settings;

	if ( $one_settings['feed_url'] ) :
		$feedurl = $one_settings['feed_url'];
		$outputarray = array( 'rss' => $feedurl, 'rss2' => $feedurl, 'atom' => $feedurl, 'rdf' => $feedurl, 'comments_rss2' => '' );
		$outputarray[$feed] = $feedurl;
		$output = $outputarray[$feed];
	endif;

	return $output;
}

/**
 * Filters the category, author, and tag feed links.
 * This changes all of these feed links to the user's alternate feed URL.
 * This change only happens if the user chooses it from the theme settings.
 *
 * @since 0.4
 * @return string $link
 */
function one_other_feed_link( $link ) {
	global $one_settings;

	if ( $one_settings['feeds_redirect'] && $one_settings['feed_url'] ) :
		$link = $one_settings['feed_url'];
	endif;

	return $link;
}

/**
 * Dynamic element to wrap the site title in.
 * If it is the home page, wrap it in an 'h1' element.
 * One other pages, wrap it in a 'div' element.
 * Use the one_site_title filter to customize.
 *
 * @uses get_bloginfo() Grabs the URL and site name
 * @link http://codex.wordpress.org/Template_Tags/get_bloginfo
 *
 * @since 0.1
 */
function one_site_title() {
	if ( is_home() || is_front_page() )
		$tag = 'h1';
	else
		$tag = 'div';

	$name = '<' . $tag . ' id="site-title">';
	$name .= '<a href="' . get_bloginfo( 'url' ) . '" title="' . get_bloginfo( 'name' ) . '"><span>' . get_bloginfo( 'name' ) . '</span></a>';
	$name .= '</' . $tag . '>';

	echo apply_filters( 'one_site_title', $name );
}

/**
 * Dynamic element to wrap the site description in.
 * If it is the home page, wrap it in an 'h2' element.
 * One other pages, wrap it in a 'div' element.
 * Use the one_site_description filter to customize.
 *
 * @uses get_bloginfo() Grabs the site description (tagline)
 * @link http://codex.wordpress.org/Template_Tags/get_bloginfo
 *
 * @since 0.1
 */
function one_site_description() {
	if ( is_home() || is_front_page() )
		$tag = 'h2';
	else
		$tag = 'div';

	$desc = "\n\t\t\t" . '<' . $tag . ' id="site-description">';
	$desc .= '<span>' . get_bloginfo( 'description' ) . '</span>';
	$desc .= '</' . $tag . '>' . "\n";

	echo apply_filters( 'one_site_description', $desc );
}

/**
 * 显示网页导航菜单。
 * @连接 http://codex.wordpress.org/Template_Tags/wp_page_menu
 *
 * @since 1.0.0
 */
function one_page_nav() {
	echo '<div id="navigation">';

	one_before_page_nav(); //钩子

		$args = array(
			'show_home' => __('首页','one'),
			'menu_class' => 'page-nav',
			'sort_column' => 'menu_order',
			'depth' => 1,
			'echo' => 0,
		);

		$nav = str_replace( array( "\r", "\n", "\t" ), '', wp_page_menu( $args ) );

		$nav = str_replace( '<div class="', '<div id="page-nav" class="', $nav );

		$nav = preg_replace( '/<ul>/', '<ul class="menu sf-menu">', $nav, 1 );

		echo apply_filters( 'one_page_nav', $nav );

	one_after_page_nav(); //钩子

	echo '</div>';
}

/**
 * Displays the category navigation menu.
 * Users should filter one_category_menu if only wanting to filter links.
 * This should only be filtered if directly adding something outside of the nav list.
 *
 * The one_before_cat_nav() action hook fires just before the menu.
 * The one_after_cat_nav() action hook fires just after the menu.
 *
 * @since 0.1
 */
function one_cat_nav() {
	echo "\n\t<div id='cat-navigation'>\n\t\t";

	one_before_cat_nav(); // Before category navigation

	$nav = one_category_menu( 'menu_class=cat-nav&echo=0&depth=1' );
	echo apply_filters( 'one_cat_nav', $nav );

	one_after_cat_nav(); // After category navigation hook

	echo "\n\t</div>\n";
}

/**
 * Menu listing for categories
 * Much like WP 2.7's wp_page_menu() functionality
 * Ability to easily filter or call one template tag to show cat list
 * Add all the default arguments of wp_list_categories()
 * Always set the 'title_li' paramater to false b/c this is a menu list
 * Always set the 'echo' parameter to false b/c we need to add classes
 *
 * Allow users to filter the function in child themes
 * Filter should return an array of arguments for $cats
 * one_category_menu_args to filter arguments
 * one_category_menu to filter entire menu
 * Users can also filter wp_list_categories, the WP filter hook.
 *
 * @uses wp_list_categories() Creates a list of the site's categories
 * @link http://codex.wordpress.org/Template_Tags/wp_list_categories
 *
 * @since 0.2.3
 * @param array $args
 */
function one_category_menu( $args = array() ) {

	$defaults = array(
		'show_home' => false,
		'menu_class' => 'cat-nav',
		'style' => 'list',
		'hide_empty' => 1,
		'use_desc_for_title' => 0,
		'depth' => 1,
		'hierarchical' => true,
		'echo' => 1
	);

	// Allow filter of arguments
	$args = apply_filters( 'one_category_menu_args', $args );

	$args = wp_parse_args( $args, $defaults );
	extract( $args );

	if ( $echo )
		$echo_menu = true;

	$args['title_li'] = false;
	$args['echo'] = false;
	$args['style'] = 'list';

	if ( $show_home )
		$cats[0] = '<li class="' . $home . '"><a href="' . get_bloginfo( 'url' ) . '" title="' . $show_home . '">' . $show_home . '</a></li>';

	$cats[1] = str_replace( array( "\t", "\n", "\r" ), '', wp_list_categories( $args ) );

	$cats = join( '', $cats );

	$menu = '<div id="' . $menu_class . '" class="' . $menu_class . '"><ul class="menu sf-menu">' . $cats . '</ul></div>';

	/*
	* Users should really create their own menu instead of overwriting this
	* It's better to start from scratch if doing this much modification
	*/
	$menu = apply_filters( 'one_category_menu', $menu );

	if ( $echo_menu )
		echo $menu;
	else
		return $menu;
}

/**
 * Loads the theme search form.
 * This function is maintained for backwards compatibility with WP and one.
 * To overwrite, users should create a new 'searchform.php' in their child theme.
 *
 * In 0.5, the one_search_form filter hook was deprecated.
 *
 * WP 2.7+ users can call searchform.php with get_search_form()
 *
 * @since 0.1
 */
function one_search_form() {
	$search = false;
	$search = apply_filters( 'one_search_form', $search );

	if ( empty( $search ) )
		get_search_form();
	else
		echo $search;
}

/**
 * Displays site copyright info with link back to site.
 * Can be removed/added through theme settings.
 * This can also be customized through child themes.
 * Use the one_copyright filter hook to modify it.
 *
 * @since 0.1
 */
function one_copyright() {
		$copyright = '<p class="copyright">';
		$copyright .= '<span class="text">' . __('Copyright', 'one') . ' &#169; ' . date( __('Y', 'one') ) . '</span> <a href="' . get_bloginfo( 'url' ) . '" title="' . get_bloginfo( 'name' ) . '" rel="bookmark"><span>' . get_bloginfo( 'name' ) . '</span></a>. ';
		$copyright .= '</p>';
	echo apply_filters( 'one_copyright', $copyright );
}

/**
 * Displays one and/or WordPress credit links.
 * Can be added/removed through theme settings.
 * Use the one_credit filter hook to customize this.
 *
 * @since 0.1
 */
function one_credit() {
	$data = get_theme_data( TEMPLATEPATH . '/style.css' );

	$wp_link = '<a class="wp-link" href="http://wordpress.org" title="' . __('Powered by WordPress, state-of-the-art semantic personal publishing platform', 'one') . '"><span>' . __('WordPress', 'one') . '</span></a>';
	$th_link = '<a class="one-link" href="' . $data['URI'] . '" title="' . __('One Theme', 'one') . '"><span>' . __('one', 'one') . '</span></a>';

	$credit = '<p class="credit">' . sprintf( __('<span class="text">Powered by</span> %1$s <span class="text">and</span> %2$s. &amp; <script type="text/javascript" src="http://js.tongji.cn.yahoo.com/371775/ystat.js"></script><ins class="yahoo_tj"><noscript><del><a href="http://tongji.cn.yahoo.com"><img src="http://img.tongji.cn.yahoo.com/371775/ystat.gif" alt="yahoo" /></a></del></noscript></ins> &amp; <script type="text/javascript"  src="http://www.nasboq.com/stat?id=4351&amp;sid=it"></script>.', 'one'), $wp_link, $th_link ) . '</p>';

	echo apply_filters( 'one_credit', $credit );
}

/**
 * Gets rids of widows, making cleaner typography
 * Replaces the space between any last to words in a string
 * Hooks them together with &nbsp;
 * This causes any two last words of a paragraph to drop down together
 * Works with inline elements (a, em, strong, span, b, i)
 * Works with block elements (h1-h6, p, li)
 *
 * No longer called by default
 * Users should use add_filter( 'the_content', 'one_widow' )
 *
 * @since 0.3.1
 */
function one_widow( $text ) {
	$widow = "/([^\s])\s+(((<(a|span|i|b|em|strong|acronym|caps|sub|sup|abbr|big|small|code|cite|tt)[^>]*>)*\s*[^\s<>]+)(<\/(a|span|i|b|em|strong|acronym|caps|sub|sup|abbr|big|small|code|cite|tt)>)*[^\s<>]*\s*(<\/(p|h[1-6]|li)>|$))/i";
	return preg_replace( $widow, '$1&nbsp;$2', $text );
}

/**
 * Adds a thin space (&thinsp;) before and after dashes
 * Works with both &mdash; and &ndash;
 * A dash may have whitespace or &nbsp; or both/either side
 *
 * No longer called by default
 * Users should use add_filter( 'the_content', 'one_dash' )
 *
 * @since 0.3.1
 */
function one_dash( $text = false ) {
	$dash = "/(\s|&nbsp;|&thinsp;)*(&mdash;|&ndash;|&#x2013;|&#8211;|&#x2014;|&#8212;)(\s|&nbsp;|&thinsp;)*/";
	return preg_replace( $dash, '&thinsp;\\2&thinsp;', $text );
}

/**
 * Filters term_links-category
 * Adds the title attribute to the link
 *
 * @since 0.4
 */
function one_category_term_link( $term_links ) {
	if ( $term_links ) :
		foreach ( $term_links as $link ) :
			preg_match( '/<a.+>(.+)<\/a>/', $link, $matches );
			$title = sprintf( __('View all posts in %1$s', 'one'), $matches[1] );
			$links[] = str_replace( '<a', '<a title="' . $title . '"', $link );
		endforeach;
		return $links;
	endif;
}

/**
 * Filters term_links-post_tag
 * Adds the title attribute to the link
 *
 * @since 0.4
 */
function one_post_tag_term_link( $term_links ) {
	if ( $term_links ) :
		foreach ( $term_links as $link ) :
			preg_match( '/<a.+>(.+)<\/a>/', $link, $matches );
			$title = sprintf( __('View all posts tagged %1$s', 'one'), $matches[1] );
			$links[] = str_replace( '<a', '<a title="' . $title . '"', $link );
		endforeach;
		return $links;
	endif;
}

/**
 * Highlights the search query on search results pages
 * Currently only highlights exact matches
 *
 * @since 0.4
 */
function one_search_highlight( $content ) {
	if ( is_search() ) :
		$search_text = ' ' . attribute_escape( get_search_query() ) . ' ';
		$content = str_replace( $search_text, '<strong class="search-query">' . $search_text . '</strong>', $content );
	endif;
	return $content;
}

/**
 * Displays the default entry title
 * Wraps the title in the appropriate header tag
 * Use the one_entry_title filter to customize
 *
 * @since 0.5
 */
function one_entry_title() {

	if ( is_attachment() )
		$title = the_title( '<h1 class="attachment-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h1>', false );

	elseif ( is_page() )
		$title = the_title( '<h1 class="page-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h1>', false );

	elseif ( is_single() )
		$title = the_title( '<h1 class="single-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h1>', false );

	else
		$title = the_title( '<h2 class="post-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>', false );

	echo apply_filters( 'one_entry_title', $title );
}

/**
 * Default entry byline for posts
 * Shows the author, date, and edit link
 * Use the one_byline filter to customize
 *
 * @since 0.5
 */
function one_byline() {
	global $post;

	if ( !is_page() && !is_attachment() ) :
		$byline = '<p class="byline">';
		$byline .= sprintf( __('<span class="text">投稿人:</span> %1$s <span class="text">日期:</span> %2$s', 'one'), '<span class="author vcard"><a class="url fn n" href="' . get_author_posts_url( get_the_author_ID() ) . '" title="' . get_the_author() . '">' . get_the_author() . '</a></span>', '<abbr class="published" title="' . sprintf( get_the_time( __('Y-m-d , l , H:i', 'one') ) ) . '">' . sprintf( get_the_time( __('Y年m月d日 H:i', 'one') ) ) . '</abbr>' );
		if ( current_user_can( 'edit_posts' ) )
			$byline .= ' <span class="separator">|</span> <span class="edit"><a href="' . get_edit_post_link( $post->ID ) . '" title="' . __('Edit post', 'one') . '">' . __('Edit', 'one') . '</a></span>';
		$byline .= '</p>';
	endif;

	echo apply_filters( 'one_byline', $byline );
}

/**
 * Displays the default entry metadata
 * Shows the category, tag, and comments link
 * Use the one_entry_meta filter to customize
 *
 * @since 0.5
 */
function one_entry_meta() {

	if ( !is_singular() && comments_open() ) :
		$number = get_comments_number();
		if ( $number == 0 )
			$comments_link = ' <span class="separator">|</span> <a class="comments-link" href="' . get_permalink() . '#respond" title="' . sprintf( __('Comment on %1$s', 'one'), the_title_attribute( 'echo=0' ) ) . '">' . __('Leave a response', 'one') . '</a>';
		elseif ( $number == 1 )
			$comments_link = ' <span class="separator">|</span> <a class="comments-link" href="' . get_comments_link() . '" title="' . sprintf( __('Comment on %1$s', 'one'), the_title_attribute( 'echo=0' ) ) . '">' . __('1 Response', 'one') . '</a>';
		else
			$comments_link = ' <span class="separator">|</span> <a class="comments-link" href="' . get_comments_link() . '" title="' . sprintf( __('Comment on %1$s', 'one'), the_title_attribute( 'echo=0' ) ) . '">' . sprintf( __('%1$s Responses', 'one'), $number ) . '</a>';
	endif;

	if ( !is_page() && !is_attachment() ) :
		$metadata = '<p class="entry-meta post-meta-data">';
		$metadata .= '<span class="categories"><span class="text">' . __('Posted in', 'one') . '</span> ' . get_the_category_list( ', ' ) . '</span>';
		$metadata .= get_the_tag_list( '<span class="tags"> <span class="separator">|</span> <span class="text">' . __('Tagged', 'one') . '</span> ', ', ', '</span>' );
		$metadata .= $comments_link;
		$metadata .= '</p>';
	elseif ( is_page() ) :
		if ( current_user_can( 'edit_pages' ) ) :
			$metadata = '<p class="entry-meta post-meta-data">';
			$metadata .= '<span class="edit"><a href="' . get_edit_post_link( $post->ID ) . '" title="' . __('Edit page', 'one') . '">' . __('Edit', 'one') . '</a></span>';
			$metadata .= '</p>';
		endif;
	endif;

	echo apply_filters( 'one_entry_meta', $metadata );
}

/**
 * Displays the breadcrumb trail
 * Calls the get_the_breadcrumb() function
 *
 * Use the get_the_breadcrumb_args filter hook
 * The one_breadcrumb_args filter is deprecated
 *
 * @since 0.1
 */
function one_breadcrumb() {
	$defaults = array(
		'separator' => '/',
		'before' => '<span class="breadcrumb-title">' . __('Browse:', 'one') . '</span>',
		'after' => false,
		'front_page' => false,
		'show_home' => __('Home', 'one'),
		'echo' => true,
	);

	$args = apply_filters( 'one_breadcrumb_args', $args );
	$args = wp_parse_args( $args, $defaults );
	one_breadcrumb_trail( $args );
}


/**
  Creates a little quote icon in comment boxes which,
  when clicked, copies that comment to the comment box wrapped in blockquotes
 *
 * @since 1.0.0
 */


add_action('comment_form', 'yus_reply_js');

function yus_reply_js()
{
?>
    <script type="text/javascript">
	//<![CDATA[
    function yus_replyTo(commentID,author) {
		var inReplyTo='@<a href="#comment-'+commentID+'">'+author+':<\/a>';
    	var myField;
        if (document.getElementById('comment') && document.getElementById('comment').type == 'textarea') {
    		myField = document.getElementById('comment');
    	} else {
    		return false;
    	}
    	if (document.selection) {
    		myField.focus();
    		sel = document.selection.createRange();
    		sel.text = inReplyTo;
    		myField.focus();
    	}
    	else if (myField.selectionStart || myField.selectionStart == '0') {
    		var startPos = myField.selectionStart;
    		var endPos = myField.selectionEnd;
    		var cursorPos = endPos;
    		myField.value = myField.value.substring(0, startPos)
    					  + inReplyTo
    					  + myField.value.substring(endPos, myField.value.length);
    		cursorPos += inReplyTo.length;
    		myField.focus();
    		myField.selectionStart = cursorPos;
    		myField.selectionEnd = cursorPos;
    	}
    	else {
    		myField.value += inReplyTo;
    		myField.focus();
    	}
   	}
	//]]>
    </script>
<?php
}
function one_to_reply()
{
if (!is_feed()) {

		
		if (comments_open() && have_comments() && get_comment_type() != "pingback" && get_comment_type() != "trackback") {?>
			<span class="one_replyTo" >
				<span title="Reply to this comment" onclick='yus_replyTo("<?php comment_ID(); ?>", "<?php comment_author();?>")' style="cursor: pointer;" class="add">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
			</span>
			<?php
		}
	}
}
?>